package com.egao.api.controller.system;


import cn.hutool.core.util.StrUtil;
import com.egao.api.base.BaseApiController;
import com.egao.common.core.constants.UserConstant;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.OperRecord;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.OperRecordService;
import com.egao.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 */
@RequestMapping("/api/user")
@RestController
@Api(value = "用户API",tags = {"用户API"})
public class ApiUserController extends BaseApiController {

    @Autowired
    private UserService userService;
    @Autowired
    private OperRecordService operRecordService;

    /**
     * 添加个人信息
     */
    @PostMapping("/save")
    @ApiOperation(value = "用户保存",notes = "company_id int 公司id 默认值:4\n" +
            "username varchar 账号\n" +
            "password varchar 密码\n" +
            "sex int 性别:1男 2女\n" +
            "phone varchar 手机号\n" +
            "email varchar 邮箱\n" +
            "true_name varchar 姓名\n" +
            "id_card varchar 身份证号\n" +
            "birthday date 出生日期\n" +
            "type varchar 用户类型:客户,司机,货代",httpMethod = "POST")
    @ResponseBody
    public JsonResult save(User user, HttpServletRequest request) {
        user.setState(0);
        user.setCompanyId(UserConstant.COMPANY_ID);
        user.setType("平台用户");
        user.setAdmin("0");
        user.setPassword(userService.encodePsw(user.getPassword()));
        if (userService.saveUser(user)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 修改个人信息
     */
    @PostMapping("/update")
    @ApiOperation(value = "用户修改",notes =
            "userId varchar 用户id\n" +
            "nick_name varchar 昵称\n" +
            "sex int 性别:1男 2女\n" +
            "phone varchar 手机号\n" +
            "email varchar 邮箱\n" +
            "true_name varchar 真实姓名\n" +
            "id_card varchar 身份证号\n" +
            "birthday date 出生日期\n",httpMethod = "POST")
    @ResponseBody
    public JsonResult update(User user, HttpServletRequest request) {
        user.setState(null);  // 状态不能修改
        user.setPassword(null);  // 密码不能修改
        user.setUsername(null);  // 账号不能修改
        if (userService.updateUser(user)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }


    /**
     * 密码修改
     */
    @ApiOperation(value = "密码修改",notes = "" +
            "id (user表的id)用户id必填\n" +
            "oldPsw 原来的密码 必填\n" +
            "newPsw 新密码 必填\n" +
            "",httpMethod = "POST")
    @PostMapping("/psw/update")
    @ResponseBody
    public JsonResult update(String id, String oldPsw, String newPsw, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "密码修改", "密码修改",
                request.getServletPath(), "POST", "ApiLoginController.registerWechart", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        if (StrUtil.hasBlank(id, oldPsw, newPsw)) {
            return JsonResult.error("参数不能为空");
        }
        User u = getLoginUser(request);
        if (u == null) {
            return JsonResult.error("未登录");
        }
        User user = new User();
        user.setUserId(Integer.valueOf(id));
        user.setPassword(userService.encodePsw(newPsw));
        if (!userService.comparePsw(userService.getUserById(user).getPassword(), oldPsw)) {
            return JsonResult.error("原密码输入不正确");
        }


        return userService.updatePsw(user, u);
    }

}
